//
//  ControlTemplateViewLabelController.m
//  Uhomebk
//
//  Created by 揭婧雯 on 17/3/3.
//  Copyright © 2017年 com.segi. All rights reserved.
//

#import "ControlTemplateViewLabelController.h"

#pragma mark -
#pragma mark 文本显示控件【label】
@implementation ControlTemplateViewLabelController{
    UILabel* contentLabel;
    CustomButtonView* abtn;
    CGSize titleSize;
}

static ControlTemplateViewLabelController* sharedManagerLabelInter = nil;

+ (ControlTemplateViewLabelController*)sharedManagerLabel
{
    sharedManagerLabelInter = [[self alloc] init];
    
    return sharedManagerLabelInter;
}

/**
 *  文本显示控件初始化
 */
- (void)initVar
{
    _valueStr = @"";
    _param_id = [SEGUtils ValueToString:[_params valueForKey:@"attrInstId"]];
    _paramId = [_params valueForKey:@"attrInstId"];
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
    if ([[_params valueForKey:@"attrValue"] length] > 0) {
        _defaultValue = [_params valueForKey:@"attrValue"];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initVar];
    self.isNeedAdpterHotspot = YES;
    
    _bgMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, TEMPLATE_HEIGHT)];
    _bgMainView.backgroundColor = kColor_W;
    self.tempBgView = _bgMainView;
    [self.view addSubview:_bgMainView];
    
    UILabel* titleLabel = [[UILabel alloc] init];
    [titleLabel extendAttributedString:[_params valueForKey:@"title"] lineSpace:LINESPACE_6 font:kFont_16];
    [titleLabel setFont:kFont_16];
    [titleLabel setTextColor:kColor_1];
    titleLabel.numberOfLines = 0;
    titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [_bgMainView addSubview:titleLabel];
    [titleLabel sizeToFit];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_bgMainView.mas_left).offset(HEADSPACE);
        make.centerY.equalTo(_bgMainView.mas_centerY);
        make.width.mas_equalTo(TITLE_WIDTH);
    }];
    titleSize = [[_params valueForKey:@"title"] getAttributionHeightWithlineSpace:LINESPACE_6 font:kFont_16 width:TITLE_WIDTH];
    
    //1.不可读，隐藏控件
    if ([[SEGUtils ValueToString:[_params valueForKey:@"readable"]] isEqualToString:@"0"]) {
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), 0);
        _optional = @"0";
        return;
    }
    //2.可读 - Lable控件不区分是否可写
    UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(LINEMARGIN_LEFT, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(_bgMainView.bounds), 0.5)];
    lineView.backgroundColor = kColor_L2;
    lineView.tag = 8888;
    [_bgMainView addSubview:lineView];
    
    contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, 9, CGRectGetWidth(_bgMainView.bounds) - CONTENTMARGIN_LEFT - CONTENTMARGIN_RIGHT, TEMPLATE_HEIGHT)];
    contentLabel.numberOfLines = 0;
    contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [contentLabel extendAttributedString:[_params valueForKey:@"attrValue"] lineSpace:LINESPACE_6 font:kFont_16];
    [contentLabel setTextAlignment:NSTextAlignmentLeft];
    [contentLabel setFont:kFont_16];
    [contentLabel setTextColor:kColor_1];
    [_bgMainView addSubview:contentLabel];
    
    [self refreshUI];
}

-(void)setContenLabel
{
    
}

- (void)paddingLabelView:(NSString*)valueStr
{
    _valueStr = valueStr;
    _optional = @"0";
}

-(void)refreshUI {
    
    CGSize size = [[_params valueForKey:@"attrValue"] getAttributionHeightWithlineSpace:LINESPACE_6 font:kFont_16 width:CGRectGetWidth(contentLabel.bounds)];
    size.height = size.height > 24 ? size.height : 24;
    size.height = titleSize.height > size.height ? titleSize.height : size.height;
    if(size.height > kFont_16.lineHeight * 4 + LINESPACE_6 * 3) {//当数据超过4行的时候，进行缩成4行，点击下接箭头可以显示全部内容
        contentLabel.frame = CGRectMake(CONTENTMARGIN_LEFT, 15, CGRectGetWidth(contentLabel.bounds), kFont_16.lineHeight * 4 + LINESPACE_6 * 3);
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), CGRectGetHeight(contentLabel.bounds) + 30 + 26);
        
        abtn = [[CustomButtonView alloc] initWithFrame:CGRectMake(CONTENTMARGIN_LEFT, CGRectGetHeight(contentLabel.bounds) + 10 + 15, 35, 16)];
        [abtn.titleLabel setFont:kFont_16];
        [abtn addTarget:self action:@selector(onBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [abtn setTitle:@"全文" forState:UIControlStateNormal];
        [abtn setTitle:@"收起" forState:UIControlStateSelected];
        abtn.selected = NO;
        [abtn setTitleColor:kColor_S3 forState:UIControlStateNormal];
        [_bgMainView addSubview:abtn];
    } else {
        contentLabel.frame = CGRectMake(CONTENTMARGIN_LEFT, 15, CGRectGetWidth(contentLabel.bounds), size.height);
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), CGRectGetHeight(contentLabel.bounds) + 30);
    }
    UIView* lineView = [_bgMainView.subviews objectAtIndex:1];
    lineView.frame = CGRectMake(lineView.frame.origin.x, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(lineView.bounds), CGRectGetHeight(lineView.bounds));
}

/**
 *  当数据超过4行的时候，进行缩成4行，点击下接箭头可以显示全部内容
 */
- (void)onBtnAction:(CustomButtonView*)sender
{
    sender.selected = !sender.selected;
    CGSize size = [[_params valueForKey:@"attrValue"] getAttributionHeightWithlineSpace:LINESPACE_6 font:kFont_16 width:CGRectGetWidth(contentLabel.bounds)];
    if (sender.selected) {//已展开，显示收起
        contentLabel.frame = CGRectMake(CONTENTMARGIN_LEFT, 15, CGRectGetWidth(contentLabel.bounds), size.height);
    } else {//收起状态，显示展开
        contentLabel.frame = CGRectMake(CONTENTMARGIN_LEFT, 15, CGRectGetWidth(contentLabel.bounds), kFont_16.lineHeight * 4 + LINESPACE_6 * 3);
    }
    abtn.frame = CGRectMake(CONTENTMARGIN_LEFT, CGRectGetHeight(contentLabel.bounds) + 10 + 15, 35, 16);
    _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), CGRectGetHeight(contentLabel.bounds) + 30 + 26);
    UIView* lineView = [_bgMainView.subviews objectAtIndex:1];
    lineView.frame = CGRectMake(lineView.frame.origin.x, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(lineView.bounds), CGRectGetHeight(lineView.bounds));
    
    if(self.fundelegate && [self.fundelegate respondsToSelector:@selector(onFeeRefeshUI)]) {
        [self.fundelegate onFeeRefeshUI];
    }
}
@end
